package mysql

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"gorm.io/gorm"
	"web_app/models"
	"web_app/tools/paginate"
)

func UserCreate(u *models.User) error {
	return DB.Model(&models.User{}).Create(u).Error
}

func UserList(ctx *gin.Context) ([]models.UserBankList, error) {
	var u []models.UserBankList
	res := GetDb(ctx).Model(&models.User{}).Scopes(paginate.Paginate(ctx)).Preload("UserBank").Find(&u)

	if errors.Is(res.Error, gorm.ErrRecordNotFound) {
		return u, errors.New("查询数据不存在 LR~")
	}

	return u, res.Error
}

/*func GetuserList(offset, limit int, search User) (users []User, count int, err error) {
	if search.Name != "" {
		 = u.mysql.Where("name LIKE ?", search.Name+"%")
	}
	if search.Category != "" {
		u.mysql = u.mysql.Where("age =?", search.Age)
	}
	err = u.mysql.Offset(offset).Limit(limit).Find(&users).Offset(-1).Limit(-1).Count(&count).Error
	return users, count, err
}*/

func UserTags(ctx *gin.Context) ([]models.UserTagList, error) {
	var ut []models.UserTagList
	tx := DB.Model(&models.User{})
	var count int64
	//tx.Where("user_tag.user_id > ?", 100) //正确的判断
	res := tx.Select("user.`id`,user.username,user_tag.`tag_name`").
		Joins("left join user_tag on user_tag.user_id=user.id ").Find(&ut).Offset(-1).Limit(-1).Count(&count)
	fmt.Println("当前数据总数为：", count)
	return ut, res.Error
}
